package com.xhtt.modules.reportform.service.impl;


import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xhtt.modules.reportform.dao.KpiReportMapper;
import com.xhtt.modules.reportform.model.UserTaskCount;
import com.xhtt.modules.reportform.service.KpiReportService;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * <p>
 * 统计分析-KPI
 * </p>
 *
 * @author xj
 * @since 2019-02-14
 */
@Service
public class KpiReportServiceImpl extends ServiceImpl<KpiReportMapper, Object> implements KpiReportService {

    @Override
    public List<Map<String, Object>> team(Map<String, Object> params) {
        return baseMapper.queryTeam(params);
    }

    @Override
    public List<Map<String, Object>> time(Map<String, Object> params) {
        return baseMapper.queryTime(params);
    }

    @Override
    public List<UserTaskCount> user(Map<String, Object> params) {
        List<UserTaskCount> userMainList = baseMapper.queryUserMain(params);
        List<UserTaskCount> userAssistList = baseMapper.queryUserAssist(params);
        Set<Integer> userIdSet = new HashSet<>();

        List<UserTaskCount> resultList = new ArrayList<>();
        for (UserTaskCount utc : userMainList) {
            if (!userIdSet.contains(utc.getUserId())) {
                userIdSet.add(utc.getUserId());
                resultList.add(new UserTaskCount(utc.getUserId(), utc.getName(), utc.getMainCount()));
            }
        }
        for (UserTaskCount utc : userAssistList) {
            if (!userIdSet.contains(utc.getUserId())) {
                userIdSet.add(utc.getUserId());
                resultList.add(new UserTaskCount(utc.getAssistCount(), utc.getUserId(), utc.getName()));
            } else {
                UserTaskCount c = resultList.stream().filter(cc -> cc.getUserId().intValue() == utc.getUserId().intValue()).findFirst().get();
                c.setAssistCount(utc.getAssistCount());
            }
        }

//        for(UserTaskCount utc : resultList)
//        {
//            for (UserTaskCount m : userMainList) {
//                if(utc.getUserId().intValue()==m.getUserId().intValue())
//                {
//                    utc.setMianCount(m.getMianCount());
//                }
//            }
//            for (UserTaskCount a : userAssistList) {
//                if(utc.getUserId().intValue()==a.getUserId().intValue())
//                {
//                    utc.setMianCount(a.getAssistCount());
//                }
//            }
//        }
        return resultList;
    }

    @Override
    public List<Map<String, Object>> carUser(Map<String, Object> params) {
        return baseMapper.queryCarUser(params);
    }

}
